package com.zhentao.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.pojo.Menu;
import com.zhentao.pojo.RoleMenu;
import com.zhentao.service.impl.MenuServiceImpl;
import com.zhentao.service.impl.RoleMenuServiceImpl;
import com.zhentao.util.LoginVO;
import com.zhentao.pojo.User;
import com.zhentao.pojo.UserRole;
import com.zhentao.service.impl.UserRoleServiceImpl;
import com.zhentao.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author "杨旭朋"
 * @ClassName: ManpowerController
 * @date 2025年07月29日 19:55
 */
@RequestMapping("manpower")
@RestController
public class ManpowerController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private UserRoleServiceImpl userRoleService;
    @Autowired
    private RoleMenuServiceImpl roleMenuService;
    @Autowired
    private MenuServiceImpl menuService;

    /**
     * @title: login
     * @desc: 登陆
     * @params ()
     * @return: Result
     * @author: 杨旭朋
     * @date: 2025/7/30 10:46
     */
    @PostMapping("login")
    public String login(@RequestBody LoginVO loginVO) {
        // 根据用户名查询用户
        User one = userService.getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, loginVO.getUsername()));
        // 检查用户是否存在
        if (one == null) {
            return "用户不存在";
        }
        // 检查用户是否被禁用
        if (one.getStatus() == 0) {
            return "用户被禁用";
        }
        // 验证密码（使用BCrypt的checkpw方法直接验证原始密码和数据库中的加密密码）
        if (BCrypt.checkpw(loginVO.getPassword(), one.getPassword())) {
            return "登陆成功";
        } else {
            return "密码错误";
        }
    }


    /**
     * @title: register
     * @desc: 注册
     * @params ()
     * @return: Result
     * @author: 杨旭朋
     * @date: 2025/7/30 10:47
     */
    @PostMapping("register")
    public String register(@RequestBody LoginVO loginVO) {
        User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, loginVO.getUsername()));
        if (null != one) {
            return "用户已存在";
        }
        User save = new User();
        String gensalt = BCrypt.gensalt();
        save.setUsername(loginVO.getUsername());
        save.setPassword(BCrypt.hashpw(loginVO.getPassword(), gensalt));
        save.setSalt(gensalt);
        save.setStatus(1);
        save.setRealName("用户" + loginVO.getUsername());
        userService.save(save);
        UserRole userRole = new UserRole();
        userRole.setUserId(save.getId());
        userRole.setRoleId(Long.valueOf("6"));
        userRoleService.save(userRole);
        return "注册成功";
    }

    @PostMapping("getMenuList")
    public List<Menu> getMenuList(@RequestParam("username") String username) {
        User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
        UserRole role = userRoleService.getOne(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, one.getId()));
        List<RoleMenu> list = roleMenuService.list(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, role.getRoleId()));
        ArrayList<Menu> menus = new ArrayList<>();
        for (RoleMenu roleMenu : list) {
            for (Menu menu : menuService.list()) {
                if (roleMenu.getMenuId().equals(menu.getId())) {
                    menus.add(menu);
                }
            }
        }
        ArrayList<Menu> parentList = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                parentList.add(menu);
            }
        }
        for (Menu menu : parentList) {
            ArrayList<Menu> child = new ArrayList<>();
            for (Menu menu1 : menus) {
                if (menu1.getParentId().equals(menu.getId())) {
                    child.add(menu1);
                }
                menu.setChildren(child);
            }
        }
        return parentList;
    }

    @PostMapping("loginUser")
    public User loginUser(@RequestParam("username") String username) {
        return userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }
}

